<?php
/**
 * Created by PhpStorm.
 * User: kcj
 * Date: 2017/6/7
 * Time: 15:14
 */

namespace app\index\controller;

use think\Controller;

require_once APP_PATH."common/wxlib/WxPay.Api.php";
require_once APP_PATH."common/WxPay.JsApiPay.php";
require_once APP_PATH."common/PayNotifyCallBack.php";
require_once APP_PATH."common/wxlib/log.php";

class Recharge extends Controller
{
    public function index()
    {
        ini_set('date.timezone','Asia/Shanghai');
//error_reporting(E_ERROR);
//初始化日志
        $logHandler= new \CLogFileHandler(LOG_PATH."wxpaylogs/".date('Y-m-d').'.log');
        $log = \Log::Init($logHandler, 15);


//①、获取用户openid
        $tools = new \JsApiPay();
        $openId = $tools->GetOpenid();

        $user = db('user')->field('diamond')->where("openid='{$openId}'")->find();
        $this->assign("diamond",$user['diamond']);
        $this->assign("oid",$openId);
        $this->assign("formurl",url('/index/recharge/pay','',false));
        return view('/recharge');
    }

    public function showrecharge()
    {
        print_r(db()->query("SELECT * FROM `ms_recharge` WHERE  (  orderid='145022470220170608153500' ) "));
    }



    public function pay()
    {
        $id = $_REQUEST['rcid'];
        $openId = $_REQUEST['oid'];
        ini_set('date.timezone','Asia/Shanghai');
//error_reporting(E_ERROR);
//初始化日志
        $logHandler= new \CLogFileHandler(LOG_PATH."wxpaylogs/".date('Y-m-d').'.log');
        $log = \Log::Init($logHandler, 15);


//①、获取用户openid
        $tools = new \JsApiPay();

        $user = db('user')->where("openid='{$openId}'")->find();
        $count = db('user')->alias('u')->join('rechargeuserconfig r','u.usertype = r.usertype')->count();
        if($count==0)
        {
            $this->error("充值账号出问题了，请联系客服吧");
            return false;
        }
//②、统一下单
        $rtype = db('rechangeconfig')->where("id={$id}")->find();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($rtype['diamond']);
        $input->SetAttach($rtype['money']);
        $orderid = \WxPayConfig::MCHID.date("YmdHis");
        $input->SetOut_trade_no($orderid);
        $input->SetTotal_fee($rtype['money']*100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag($rtype['diamond']);
        $input->SetNotify_url('http://ms.redli366.com/api.php/index/recharge/notice');
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = \WxPayApi::unifiedOrder($input);
        $jsApiParameters = $tools->GetJsApiParameters($order);


        $data['uid']=$openId;
        $data['money']=$rtype['money'];
        $data['type']=0;
        $data['diamond']=$rtype['diamond'];
        $data['orderid']=$orderid;
        $data['time']=date("Y-m-d H:i:s",time());
        db('recharge')->insert($data);

        $this->assign("jsApiParameters",$jsApiParameters);
        $this->assign("diamond",$rtype['diamond']);
        $this->assign("money",$rtype['money']);
        return view('/pay');
    }

    public function notice()
    {
        \Log::DEBUG("begin notify");
        $notify = new \PayNotifyCallBack();
        $notify->Handle(false);
    }

}